import 'package:flutter/material.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../entity/home/content/cateslide_group_entity.dart';

class DecorationSliderGroup extends StatelessWidget {
  final CateslideGroupEntity model; // 数据源

  // 构造函数，添加 key 参数
  const DecorationSliderGroup({super.key, required this.model});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(
        model.pageMarginLeft!.toDouble(),
        model.pageMarginTop!.toDouble(),
        model.pageMarginRight!.toDouble(),
        model.pageMarginBottom!.toDouble(),
      ),
      padding: EdgeInsets.all(model.pageMarginIner!.toDouble()),
      child: model.slideSetting == '1' ? HorizontalListView(model: model) : VerticalListView(model: model),
    );
  }
}

class HorizontalListView extends StatelessWidget {
  final CateslideGroupEntity model; // 数据源
  const HorizontalListView({super.key, required this.model});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity, // 确保容器填满可用空间
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal, // 设置为横向滚动
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: model.subEntry!.map((entry) {
            return ItemView(model: entry, sliderGroupModel: model);
          }).toList(),
        ),
      ),
    );
  }
}

class VerticalListView extends StatelessWidget {
  final CateslideGroupEntity model; // 数据源

  const VerticalListView({super.key, required this.model}); // 默认每行2个项

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 0, // 横向间距
      runSpacing: 10, // 纵向间距
      children: model.subEntry!.map((entry) {
        return SizedBox(
          width: SliderUtils.itemW(model),
          child: ItemView(
            model: entry,
            sliderGroupModel: model,
          ),
          // child: Container(color: Colors.red, height: 50),
        );
      }).toList(),
    );
  }
}

class ItemView extends StatelessWidget {
  final CateslideGroupEntity sliderGroupModel; // 数据源
  final CateslideGroupSubEntry model;
  const ItemView({
    super.key,
    required this.sliderGroupModel,
    required this.model,
  });

  @override
  Widget build(BuildContext context) {
    return sliderGroupModel.primod == '0'
        ? InkWell(
            onTap: () {
              Tools.jumpPage(model.toJson());
            },
            child: ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(sliderGroupModel.borderRadiusType! == '1' ? 0 : sliderGroupModel.radiusNum!.toDouble())),
              child: SizedBox(
                width: SliderUtils.picW(sliderGroupModel),
                child: Stack(
                  children: [
                    ItemImageView(model: model, item: sliderGroupModel),
                    Positioned(
                      bottom: 10,
                      right: sliderGroupModel.goodsLr!.toDouble(),
                      left: sliderGroupModel.goodsLr!.toDouble(),
                      child: ItemTextView(item: sliderGroupModel, model: model),
                    ),
                  ],
                ),
              ),
            ),
          )
        : ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(sliderGroupModel.borderRadiusType! == '1' ? 0 : sliderGroupModel.radiusNum!.toDouble())),
            child: Container(
              width: SliderUtils.picW(sliderGroupModel),
              margin: EdgeInsets.fromLTRB(
                sliderGroupModel.goodsMargin!.toDouble() / 2,
                0,
                sliderGroupModel.goodsMargin!.toDouble() / 2,
                0,
              ),
              child: InkWell(
                onTap: () {
                  Tools.jumpPage(model.toJson());
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    ItemImageView(model: model, item: sliderGroupModel),
                    const SizedBox(height: 10),
                    ItemTextView(item: sliderGroupModel, model: model),
                  ],
                ),
              ),
            ),
          );
  }
}

class ItemImageView extends StatelessWidget {
  final CateslideGroupEntity item; // 数据源
  final CateslideGroupSubEntry model;
  const ItemImageView({
    super.key,
    required this.item,
    required this.model,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: item.imageFillStyle! == '1' ? const EdgeInsets.all(0) : const EdgeInsets.all(10),
      child: Center(
        child: Image.network(
          model.imageUrl!,
          fit: BoxFit.fill,
          width: SliderUtils.picW(item),
          height: SliderUtils.picH(item),
        ),
      ),
    );
  }
}

class ItemTextView extends StatelessWidget {
  final CateslideGroupEntity item; // 数据源
  final CateslideGroupSubEntry model;
  const ItemTextView({
    super.key,
    required this.model,
    required this.item,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: item.textAlignType! == 'left'
          ? Alignment.centerLeft
          : item.textAlignType! == 'center'
              ? Alignment.center
              : Alignment.centerRight,
      decoration: BoxDecoration(
        color: Tools.fromHex(item.titleBgcolor!),
        // color: Colors.red,
        borderRadius: BorderRadius.all(Radius.circular(item.btnRdx!.toDouble())),
      ),
      padding: EdgeInsets.only(
        left: item.goodsInerlr!.toDouble(),
        right: item.goodsInerlr!.toDouble(),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: item.txtMod2 == '1' ? MainAxisAlignment.start : MainAxisAlignment.center,
        children: [
          Text(
            model.title!,
            style: TextStyle(
              color: Tools.fromHex(item.titleColor!),
              fontSize: double.parse(item.titleSize!),
              fontWeight: item.textStyleType! == '1' ? FontWeight.normal : FontWeight.bold,
            ),
          ),
          SizedBox(height: item.subA!.toDouble()),
          Text(
            model.desc!,
            style: TextStyle(
              color: Tools.fromHex(item.titleColor2!),
              fontSize: item.sizeA!.toDouble(),
              decoration: item.showBd == false ? TextDecoration.none : TextDecoration.underline, // 添加下划线
              decorationColor: Tools.fromHex(item.titleColor2!),
            ),
          ),
        ],
      ),
    );
  }
}

class SliderUtils {
  static double itemW(CateslideGroupEntity item) {
    final tempW = (ScreenUtil().screenWidth - item.pageMarginLeft!.toDouble() - item.pageMarginRight!.toDouble()) / item.size!;
    final itemW = item.slideSetting == '1' ? ScreenUtil().screenWidth * item.per! / 100.0 : tempW;
    return itemW;
  }

  static double itemH(CateslideGroupEntity item) {
    final itemW = SliderUtils.itemW(item);
    switch (item.displayScale) {
      case '0':
        return itemW.toDouble() / 1.5;
      case '1':
        return itemW.toDouble();
      case '2':
        return itemW.toDouble() / 0.75;
      case '3':
        return itemW.toDouble() / (4 / 3);
      default:
        return itemW.toDouble();
    }
  }

  // 计算图片宽度
  static double picW(CateslideGroupEntity item) {
    final itemW = SliderUtils.itemW(item);
    final spaceW = item.imageFillStyle! == '1' ? 0 : 10;
    return itemW - spaceW;
  }

  // 计算图片高度
  static double picH(CateslideGroupEntity item) {
    final itemW = SliderUtils.picW(item);
    switch (item.displayScale) {
      case '0':
        return itemW.toDouble() / 1.5;
      case '1':
        return itemW.toDouble();
      case '2':
        return itemW.toDouble() / 0.75;
      case '3':
        return itemW.toDouble() / (4 / 3);
      default:
        return itemW.toDouble();
    }
  }
}
